package com.jintu.portal.utils;

import com.jintu.portal.pojo.dto.TobUserDto;
import com.jintu.portal.pojo.dto.UserDto;

import java.io.Serializable;
import java.util.Set;

/**
 * Created by liyou on 2017/9/25.
 */
public class UserUtil implements Serializable {

    public static final String USER_TOKEN_KEY_HEADER = "user:session";

    public static final String USER_COOKIE_ATTR_KEY = "USER_COOKIE_ATTR_KEY";

    public static final String OP_USER_COOKIE_KEY = "OP_TOKEN";

    public static final String CA_USER_COOKIE_KEY = "CA_TOKEN";

    public static final Integer ADMIN_USER_EXPIRE = 1800;

    public static final Integer TOB_USER_EXPIRE = 15 * 24 * 60 * 60;

    public static final String USER_TOKEN_REQ_KEY = "TOB_TOKEN";

    public static final String OP_USER_AGENCY__KEY = "OP_AGENCY_TOKEN";

    public static final String AGENT_USER_COOKIE_ATTR_KEY = "AGENT_USER_COOKIE_ATTR_KEY";

    public static final String AGENT_USER_COOKIE_KEY = "AGENT_TOKEN";

    /**
     * 获取reids管理员信息
     * @return
     */
    public static UserDto currentUser() {
        Object cookie = SessionUtil.getRequest().getAttribute(USER_COOKIE_ATTR_KEY);
        if(cookie == null) {
            return null;
        }
        String key = SessionUtil.getCookieByName(cookie.toString());
        if(key == null) {
            return null;
        }
        return RedisUtil.INSTANCE.get(key, UserDto.class);
    }

    /**
     * 更新reids管理员信息
     * @return
     */
    public static void setCurrentUser(UserDto userDto) {
        Object cookie = SessionUtil.getRequest().getAttribute(USER_COOKIE_ATTR_KEY);
        if(cookie == null) {
            return;
        }
        String key = SessionUtil.getCookieByName(cookie.toString());
        if(key == null) {
            return;
        }
        RedisUtil.INSTANCE.set(key, userDto, UserUtil.ADMIN_USER_EXPIRE);
    }

    /**
     * 移除reids管理员信息
     * @param userId
     */
    public static void removeAdminUser(Long userId) {
        userId = userId == null ? (currentUser() == null ? null : currentUser().getId()) : userId;
        if(userId == null) {
            return;
        }
        Set<String> keySet = RedisUtil.INSTANCE.keys(UserUtil.USER_TOKEN_KEY_HEADER + ":*:user:" + userId);
        for (String key : keySet) {
            if(RedisUtil.INSTANCE.exists(RedisUtil.INSTANCE.get(key))) {
                RedisUtil.INSTANCE.delete(RedisUtil.INSTANCE.get(key));
            }
            RedisUtil.INSTANCE.delete(key);
        }
        SessionUtil.getRequest().removeAttribute("currentUser");
    }
    /**
     * 强制退出B端
     * @param userId
     */
    public static void removeTobUser(Long userId) {
        userId = userId == null ? tobCurrentUser().getUserId() : userId;
        Set<String> keySet = RedisUtil.INSTANCE.keys(UserUtil.USER_TOKEN_KEY_HEADER + ":*:user:" + userId);
        for (String key : keySet) {
            if(RedisUtil.INSTANCE.exists(RedisUtil.INSTANCE.get(key))) {
                RedisUtil.INSTANCE.delete(RedisUtil.INSTANCE.get(key));
            }
            RedisUtil.INSTANCE.delete(key);
        }
    }

//    /**
//     * 获取生活号reids管理员信息
//     * @return
//     */
//    public static UserDto currentOpUser() throws Exception {
//        String openId = AlipayUtil.getAlipayOpenId();
//        String key = OP_USER_AGENCY__KEY+openId;
//        UserDto userDto =  RedisUtil.INSTANCE.get(key, UserDto.class);
//        return userDto;
//    }
//
//    /**
//     * 更新生活号reids管理员信息
//     * @return
//     */
//    public static void setCurrentOpUser(UserDto userDto) throws Exception {
//        String openId = AlipayUtil.getAlipayOpenId();
//        String key = OP_USER_AGENCY__KEY+openId;
//        RedisUtil.INSTANCE.set(key, userDto, UserUtil.ADMIN_USER_EXPIRE);
//    }
//    /**
//     * 删除生活号reids管理员信息
//     * @return
//     */
//    public static void removeOpUser() throws Exception {
//        String openId = AlipayUtil.getAlipayOpenId();
//        String key = OP_USER_AGENCY__KEY+openId;
//        if(null != RedisUtil.INSTANCE.get(key)){
//            if(RedisUtil.INSTANCE.exists(RedisUtil.INSTANCE.get(key))) {
//                RedisUtil.INSTANCE.delete(RedisUtil.INSTANCE.get(key));
//            }
//            RedisUtil.INSTANCE.delete(key);
//        }
//        SessionUtil.getRequest().removeAttribute("currentOpUser");
//    }
//
//    /**
//     * 重置生活号redis管理员信息过期时间
//     */
//    public static void opUserExpireReset() throws Exception {
//        String openId = AlipayUtil.getAlipayOpenId();
//        String key = OP_USER_AGENCY__KEY+openId;
//        RedisUtil.INSTANCE.expire(key, ADMIN_USER_EXPIRE);
//    }


    /**
     * 获取reidsB端用户信息
     * @return
     */
    public static TobUserDto tobCurrentUser() {
        Object obj = SessionUtil.getRequest().getAttribute(USER_TOKEN_REQ_KEY);
        if(obj == null) {
            return null;
        }
        String token = obj.toString();
        return RedisUtil.INSTANCE.get(token, TobUserDto.class);
    }

    /**
     * 获取reidsB端用户信息
     * @return
     */
    public static TobUserDto tobCurrentUser1(String token) {
        return RedisUtil.INSTANCE.get(token, TobUserDto.class);
    }

    /**
     * 更新reids管理员信息
     * @return
     */
    public static void setTobCurrentUser(TobUserDto tobUserDto) {
        String token = SessionUtil.getRequest().getAttribute(USER_TOKEN_REQ_KEY).toString();
        Set<String> keySet = RedisUtil.INSTANCE.keys(token + ":*");
        keySet.add(token);
        for (String key : keySet) {
            if(tobUserDto.getToken().equals(key)){
                RedisUtil.INSTANCE.set(key, tobUserDto, UserUtil.TOB_USER_EXPIRE);
            }
        }
    }
    /**
     * 重置redis管理员信息过期时间
     */
    public static void adminUserExpireReset() {
        Object cookie = SessionUtil.getRequest().getAttribute(USER_COOKIE_ATTR_KEY);
        if(cookie == null) {
            return;
        }
        String token = SessionUtil.getCookieByName(cookie.toString());
        Set<String> keySet = RedisUtil.INSTANCE.keys(token + ":*");
        keySet.add(token);
        for (String key : keySet) {
            RedisUtil.INSTANCE.expire(key, ADMIN_USER_EXPIRE);
        }
    }

    /**
     * 重置B端用户信息过期时间
     */
    public static void tobUserExpireReset() {
        String token = SessionUtil.getRequest().getAttribute(USER_TOKEN_REQ_KEY).toString();
        Set<String> keySet = RedisUtil.INSTANCE.keys(token + ":*");
        keySet.add(token);
        for (String key : keySet) {
            RedisUtil.INSTANCE.expire(key, TOB_USER_EXPIRE);
        }
    }

    /**
     * 按照角色删除redis用户信息
     * @param roleId
     */
    public static void expireRole(Long roleId) {
                Set<String> keySet = RedisUtil.INSTANCE.keys(USER_TOKEN_KEY_HEADER + ":*:role:" + roleId + ":*");
                for (String key : keySet) {
                    RedisUtil.INSTANCE.delete(RedisUtil.INSTANCE.get(key));
                    RedisUtil.INSTANCE.delete(key);
        }
    }

    /**
     * 按照部门删除redis用户信息
     * @param departmentId
     */
    public static void expireDepartment(Long departmentId) {
        Set<String> keySet = RedisUtil.INSTANCE.keys(USER_TOKEN_KEY_HEADER + ":*:department:"+departmentId + ":*");
        for (String key : keySet) {
            RedisUtil.INSTANCE.delete(RedisUtil.INSTANCE.get(key));
            RedisUtil.INSTANCE.delete(key);
        }
    }

    /**
     * 按照用户角色删除redis用户信息
     * @param postId
     * @param userId
     */
    public static void expireUserPost(Long departmentId, Long postId, Long userId) {
        Set<String> keySet = RedisUtil.INSTANCE.keys(USER_TOKEN_KEY_HEADER + ":*:department:"+departmentId+":post:"+postId + ":user:"+userId);
        for (String key : keySet) {
            RedisUtil.INSTANCE.delete(RedisUtil.INSTANCE.get(key));
            RedisUtil.INSTANCE.delete(key);
        }
    }

    /**
     * 按照用户删除redis用户信息
     * @param userId
     */
    public static void expireUser(Long userId) {
        Set<String> keySet = RedisUtil.INSTANCE.keys(USER_TOKEN_KEY_HEADER + ":*:user:"+userId);
        for (String key : keySet) {
            RedisUtil.INSTANCE.delete(RedisUtil.INSTANCE.get(key));
            RedisUtil.INSTANCE.delete(key);
        }
    }

    /**
     * 按照公司删除redis用户信息
     * @param companyId
     */
    public static void expireCompany(Long companyId) {
        Set<String> keySet = RedisUtil.INSTANCE.keys(USER_TOKEN_KEY_HEADER + ":*:company:"+companyId+":*");
        for (String key : keySet) {
            RedisUtil.INSTANCE.delete(RedisUtil.INSTANCE.get(key));
            RedisUtil.INSTANCE.delete(key);
        }
    }

//    /**
//     * 获取reids管理员信息(合作商系统)
//     * @return
//     */
//    public static TCooperatorSeller currentUserAgent() {
//        Object cookie = SessionUtil.getRequest().getAttribute(AGENT_USER_COOKIE_ATTR_KEY);
//        if(cookie == null) {
//            return null;
//        }
//        String key = SessionUtil.getCookieByName(cookie.toString());
//        if(key == null) {
//            return null;
//        }
//        return RedisUtil.INSTANCE.get(key, TCooperatorSeller.class);
//    }

//    /**
//     * 移除reids管理员信息(合作商系统)
//     * @param userId
//     */
//    public static void removeAdminUserAgent(Long userId) {
//        userId = userId == null ? (currentUserAgent() == null ? null : currentUserAgent().getId()) : userId;
//        if(userId == null) {
//            return;
//        }
//        Set<String> keySet = RedisUtil.INSTANCE.keys(UserUtil.USER_TOKEN_KEY_HEADER + ":*:agent:" + userId);
//        for (String key : keySet) {
//            if(RedisUtil.INSTANCE.exists(RedisUtil.INSTANCE.get(key))) {
//                RedisUtil.INSTANCE.delete(RedisUtil.INSTANCE.get(key));
//            }
//            RedisUtil.INSTANCE.delete(key);
//        }
//        SessionUtil.getRequest().removeAttribute("currentAgentUser");
//    }

    /**
     * 重置redis管理员信息过期时间(合作商系统)
     */
    public static void adminUserExpireResetAgent() {
        Object cookie = SessionUtil.getRequest().getAttribute(AGENT_USER_COOKIE_ATTR_KEY);
        if(cookie == null) {
            return;
        }
        String token = SessionUtil.getCookieByName(cookie.toString());
        Set<String> keySet = RedisUtil.INSTANCE.keys(token + ":*");
        keySet.add(token);
        for (String key : keySet) {
            RedisUtil.INSTANCE.expire(key, ADMIN_USER_EXPIRE);
        }
    }

}
